package controllers

import (
	"2302a_book/models"
	beego "github.com/beego/beego/v2/server/web"
)

type BookController struct {
	beego.Controller
}
type Res struct {
	Code int         `json:"code"`
	Data interface{} `json:"data"`
	Mes  string      `json:"mes"`
}

func (c *BookController) List() {
	c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
	var books []models.Book
	models.DB.Debug().Find(&books)
	c.Data["json"] = Res{
		Code: 200,
		Data: books,
		Mes:  "展示成功",
	}
	c.ServeJSON()
	return
}
func (c *BookController) Update() {
	id, _ := c.GetInt("id")
	writername := c.GetString("writername")
	writerid, _ := c.GetInt("writerid")
	name := c.GetString("name")
	bookid, _ := c.GetInt("bookid")
	books := models.Book{
		Writername: writername,
		Writerid:   writerid,
		Name:       name,
		Bookid:     bookid,
	}
	models.DB.Debug().Where("id=?", id).Updates(&books)
	c.Data["json"] = Res{
		Code: 200,
		Data: nil,
		Mes:  "修改成功",
	}
	c.ServeJSON()
	return
}
func (c *BookController) Del() {
	c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
	id, _ := c.GetInt("id")
	models.DB.Debug().Where("id=?", id).Delete(&models.Book{})
	c.Data["json"] = Res{
		Code: 200,
		Data: nil,
		Mes:  "删除成功",
	}
	c.ServeJSON()
	return
}
